<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <el-button
          type="primary"
          class="handle-del mr10"
          @click="handleAdd"
          v-if="wcmPermissions.includes('village:garbagecategory:save')"
          >新增</el-button
        >
        <el-button
          type="danger"
          class="handle-del mr10"
          @click="delAllSelection"
          v-if="wcmPermissions.includes('village:garbagecategory:delete')"
          >删除</el-button
        >
        <el-button type="primary" @click="handleSearch">搜索</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="index"
          label="序号"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="categoryName"
          label="分类名称"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="garbageType"
          label="分类编号"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="recycleType"
          label="可回收分类编号"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="unitPrice"
          label="单价"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="unitPoint"
          label="单价对应的积分"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="measureUnit"
          label="计量单位"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="memo"
          label="备注说明"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="createDate"
          label="创建时间"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="categoryVersion"
          label="版本号"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="sort"
          label="排序"
        ></el-table-column>

        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleEdit(scope.$index, scope.row)"
              v-if="wcmPermissions.includes('village:garbagecategory:update')"
              >编辑</el-button
            >
            <el-button
              type="text"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              v-if="wcmPermissions.includes('village:garbagecategory:delete')"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="50%">
      <el-form ref="form" :model="editForm" label-width="auto" :rules="rules">
        <el-form-item label="分类名称" prop="categoryName">
          <el-input
            @keyup.native.enter="saveEdit"
            v-model="editForm.categoryName"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类编号" prop="garbageType">
          <el-input
            @keyup.native.enter="saveEdit"
            v-model="editForm.garbageType"
          ></el-input>
        </el-form-item>

        <el-form-item label="可回收分类编号" prop="recycleType">
          <el-input
            @keyup.native.enter="saveEdit"
            v-model="editForm.recycleType"
          ></el-input>
        </el-form-item>

        <el-form-item label="版本号" prop="categoryVersion">
          <el-input
            @keyup.native.enter="saveAdd"
            v-model="editForm.categoryVersion"
          ></el-input>
        </el-form-item>

        <el-form-item label="单价">
          <el-input
            @keyup.native.enter="saveEdit"
            v-model="editForm.unitPrice"
          ></el-input>
        </el-form-item>

        <el-form-item label="单价对应的积分">
          <el-input
            @keyup.native.enter="saveEdit"
            v-model="editForm.unitPoint"
          ></el-input>
        </el-form-item>

        <el-form-item label="计量单位">
          <el-input
            @keyup.native.enter="saveEdit"
            v-model="editForm.measureUnit"
          ></el-input>
        </el-form-item>

        <el-form-item label="备注说明">
          <el-input
            @keyup.native.enter="saveEdit"
            v-model="editForm.memo"
          ></el-input>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input
            @keyup.native.enter="saveEdit"
            v-model="editForm.sort"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 新增弹出框 -->
    <el-dialog title="新增" :visible.sync="addVisible" width="50%">
      <el-form ref="form" :model="addForm" label-width="auto" :rules="rules">
        <el-form-item label="分类名称" prop="categoryName">
          <el-input
            @keyup.native.enter="saveAdd"
            v-model="addForm.categoryName"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类编号" prop="garbageType">
          <el-input
            @keyup.native.enter="saveAdd"
            v-model="addForm.garbageType"
          ></el-input>
        </el-form-item>

        <el-form-item label="可回收分类编号" prop="recycleType">
          <el-input
            @keyup.native.enter="saveAdd"
            v-model="addForm.recycleType"
          ></el-input>
        </el-form-item>

        <el-form-item label="版本号" prop="categoryVersion">
          <el-input
            @keyup.native.enter="saveAdd"
            v-model="addForm.categoryVersion"
          ></el-input>
        </el-form-item>

        <el-form-item label="单价">
          <el-input
            @keyup.native.enter="saveAdd"
            v-model="addForm.unitPrice"
          ></el-input>
        </el-form-item>

        <el-form-item label="单价对应的积分">
          <el-input
            @keyup.native.enter="saveAdd"
            v-model="addForm.unitPoint"
          ></el-input>
        </el-form-item>

        <el-form-item label="计量单位">
          <el-input
            @keyup.native.enter="saveAdd"
            v-model="addForm.measureUnit"
          ></el-input>
        </el-form-item>

        <el-form-item label="备注说明">
          <el-input
            @keyup.native.enter="saveAdd"
            v-model="addForm.memo"
          ></el-input>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input
            @keyup.native.enter="saveAdd"
            v-model="addForm.sort"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelAdd">取 消</el-button>
        <el-button type="primary" @click="saveAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import util from "utils/util";
import { mapGetters } from "vuex";
export default {
  name: "wastecategorymanage",
  data() {
    return {
      wcmPermissions: [],
      query: {
        name: "",
        pageIndex: 1,
        pageSize: 20
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      addVisible: false,
      pageTotal: 0,
      editForm: {
        categoryName: "",
        // categoryCode: "",
        garbageType: "",
        recycleType: "",
        unitPrice: "",
        unitPoint: "",
        measureUnit: "",
        memo: "",
        categoryVersion: "",
        sort: 0
      },
      addForm: {
        categoryName: "",
        // categoryCode: "",
        garbageType: "",
        recycleType: "",
        unitPrice: "",
        unitPoint: "",
        measureUnit: "",
        memo: "",
        categoryVersion: "",
        sort: 0
      },
      rules: {
        categoryName: [
          { required: true, message: "必填不能为空", trigger: "blur" }
        ],
        // categoryCode: [
        //   { required: true, message: "必填不能为空", trigger: "blur" }
        // ],
        garbageType: [
          { required: true, message: "必填不能为空", trigger: "blur" }
        ],
        recycleType: [
          { required: true, message: "必填不能为空", trigger: "blur" }
        ],
        categoryVersion: [
          { required: true, message: "必填不能为空", trigger: "blur" }
        ],
        sort: [{ required: true, message: "必填不能为空", trigger: "blur" }]
      },
      idx: "",
      id: "",
      ids: []
    };
  },
  created() {
    this.getWCMData();
    this.wcmPermissions = this.permissions;
  },
  computed: {
    ...mapGetters(["permissions"])
  },
  watch: {
    "$route.path": function(newVal, oldVal) {
      if (newVal == "/waste-category-manage") {
        this.wcmPermissions = this.permissions;
      }
    },
    // 刷新浏览器时触发
    permissions: function(newVal, oldVal) {
      this.wcmPermissions = newVal;
    }
  },
  methods: {
    getWCMData() {
      this.$axios
        .get("/village/garbagecategory/page", {
          params: {
            limit: this.query.pageSize,
            page: this.query.pageIndex
          }
        })
        .then(({ data: res }) => {
          console.log("垃圾品类管理数据", res);
          if (res.code != 0) {
            return;
          }
          this.tableData = res.data.list;
          this.pageTotal = res.data.total;

          this.tableData.sort((a, b) => {
            return a.sort - b.sort;
          });

          this.tableData.map((item, index) => {
            var pageIndex = this.query.pageIndex;
            item.index = index + 1 + (pageIndex - 1) * this.query.pageSize;
            item.createDate = this.dateFormat(item.createDate);
          });
        })
        .catch(() => {});
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getWCMData();
    },
    // 删除操作
    handleDelete(index, row) {
      console.log(index, row);
      this.ids = [];
      var ids = this.ids;
      ids[0] = row.id;
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning"
      })
        .then(() => {
          this.delete(ids);
        })
        .catch(() => {});
    },
    delete(ids) {
      this.$axios
        .delete("/village/garbagecategory", {
          data: ids
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return;
          }
          this.$message.success("操作成功");
          this.ids = [];
          this.getWCMData();
        })
        .catch(() => {});
    },
    // 多选操作
    handleSelectionChange(val) {
      this.ids = [];
      var ids = this.ids;
      val.forEach((item, index) => {
        ids.push(item.id);
      });
    },
    delAllSelection() {
      var ids = this.ids;
      ids = [...new Set(ids)];

      this.$confirm("确定要删除吗？", "提示", {
        type: "warning"
      })
        .then(() => {
          this.delete(ids);
        })
        .catch(() => {});
    },
    // 编辑操作
    handleEdit(index, row) {
      this.idx = index;
      this.id = row.id;
      this.editVisible = true;
      // 获取编辑数据
      this.getEdit(row.id);
    },
    getEdit(id) {
      this.$axios
        .get(`/village/garbagecategory/${id}`)
        .then(({ data: res }) => {
          console.log("编辑数据", res);
          if (res.code != 0) {
            return;
          }
          this.editForm = {
            categoryName: res.data.categoryName,
            // categoryCode: res.data.categoryCode,
            garbageType: res.data.garbageType,
            recycleType: res.data.recycleType,
            unitPrice: res.data.unitPrice,
            unitPoint: res.data.unitPoint,
            measureUnit: res.data.measureUnit,
            memo: res.data.memo,
            categoryVersion: res.data.categoryVersion,
            sort: res.data.sort
          };
        })
        .catch(() => {});
    },
    // 保存编辑
    saveEdit() {
      this.editForm = {
        categoryName: this.editForm.categoryName,
        // categoryCode: this.editForm.categoryCode,
        garbageType: this.editForm.garbageType,
        recycleType: this.editForm.recycleType,

        categoryVersion: this.editForm.categoryVersion,
        sort: this.editForm.sort
      };
      var bool = util.isEmpty(this.editForm);
      if (bool) {
        return this.$message.warning("必填项不能为空");
      }
      this.$axios
        .put("/village/garbagecategory", {
          id: this.id,
          categoryName: this.editForm.categoryName,
          // categoryCode: this.editForm.categoryCode,
          garbageType: this.editForm.garbageType,
          recycleType: this.editForm.recycleType,
          unitPrice: this.editForm.unitPrice,
          unitPoint: this.editForm.unitPoint,
          measureUnit: this.editForm.measureUnit,
          memo: this.editForm.memo,
          categoryVersion: this.editForm.categoryVersion,
          sort: this.editForm.sort
        })
        .then(({ data: res }) => {
          console.log(res);
          if (res.code != 0) {
            return;
          }
          this.editVisible = false;
          this.$message.success(`修改第 ${this.idx + 1} 行成功`);
          this.editForm = {
            categoryName: "",
            // categoryCode: "",
            garbageType: "",
            recycleType: "",
            unitPrice: "",
            unitPoint: "",
            measureUnit: "",
            memo: "",
            categoryVersion: "",
            sort: ""
          };
          this.getWCMData();
        })
        .catch(() => {});
    },
    // 新增操作
    handleAdd() {
      this.addVisible = true;
    },
    //取消新增
    cancelAdd() {
      this.addVisible = false;
      this.addForm = {
        categoryName: "",
        // categoryCode: "",
        garbageType: "",
        recycleType: "",
        unitPrice: "",
        unitPoint: "",
        measureUnit: "",
        memo: "",
        categoryVersion: "",
        sort: ""
      };
    },
    // 保存新增
    saveAdd() {
      this.addForm = {
        categoryName: this.addForm.categoryName,
        // categoryCode: this.addForm.categoryCode,
        garbageType: this.addForm.garbageType,
        recycleType: this.addForm.recycleType,
        categoryVersion: this.addForm.categoryVersion,
        sort: this.addForm.sort
      };
      var bool = util.isEmpty(this.addForm);
      if (bool) {
        return this.$message.warning("必填项不能为空");
      }
      this.$axios
        .post("/village/garbagecategory", {
          categoryName: this.addForm.categoryName,
          // categoryCode: this.addForm.categoryCode,
          garbageType: this.addForm.garbageType,
          recycleType: this.addForm.recycleType,
          unitPrice: this.addForm.unitPrice,
          unitPoint: this.addForm.unitPoint,
          measureUnit: this.addForm.measureUnit,
          memo: this.addForm.memo,
          categoryVersion: this.addForm.categoryVersion,
          sort: this.addForm.sort
        })
        .then(({ data: res }) => {
          if (res.code != 0) {
            return;
          }
          this.addVisible = false;
          this.addForm = {
            categoryName: "",
            // categoryCode: "",
            garbageType: "",
            recycleType: "",
            unitPrice: "",
            unitPoint: "",
            measureUnit: "",
            memo: "",
            categoryVersion: "",
            sort: ""
          };
          this.getWCMData();
        })
        .catch(() => {});
    },

    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getWCMData();
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getWCMData();
    }
  }
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
